
package combat.dudes.client.gui.threads.info;

import gnu.cajo.utils.extra.TransparentItemProxy;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import combat.dudes.DudesConfig;
import combat.dudes.DudesFunctions;
import combat.dudes.client.ClientApp;
import combat.dudes.client.gui.ClientGUITheme;
import combat.dudes.client.gui.images.Painter;
import combat.dudes.client.gui.threads.shell.GUIShell;
import combat.dudes.protocol.data.DoublePoint;
import combat.dudes.protocol.data.PhysicalObject;
import combat.dudes.protocol.interfaces.PhysicalObjectInt;
import combat.dudes.protocol.interfaces.ProjectileInt;
import combat.dudes.protocol.interfaces.VehicleInt;
import combat.dudes.protocol.types.PhysicalObjectType;
import combat.dudes.protocol.types.ProjectileType;
import combat.dudes.protocol.types.VehicleType;


/**
 * @author Laurens Andriessen
 * @version 1.0
 */
public class GUIInfo {
	public GUIInfo() {

		final JPanel myPanel = GUIShell.windowInfo.myBodyPanel;

		// Create tab container
		final JTabbedPane tabContainer = new JTabbedPane();
		tabContainer.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		tabContainer.setVisible(true);
		tabContainer.setBounds(0, -2, myPanel.getWidth(), myPanel.getHeight());

		// Set tab-container-background
		JPanel tabContainerFg = new JPanel() {
			private static final long serialVersionUID = 1L;

			@Override
			public void paintComponent(Graphics g) {
				g.setColor(Color.BLACK);
				g.drawLine(0, 0, myPanel.getWidth(), 0);
			}
		};
		tabContainerFg.setBounds(0, 23, myPanel.getWidth(), 23);
		tabContainerFg.setVisible(true);

		JPanel objectInfoPanel = new JPanel(null);
		JPanel ordersInfoPanel = new JPanel(null);
		JPanel settingsInfoPanel = new JPanel(null);

		ClientGUITheme.addTab(tabContainer, objectInfoPanel, "Info", "magnifier.png");
		ClientGUITheme.addTab(tabContainer, ordersInfoPanel, "Orders", "script.png");
		ClientGUITheme.addTab(tabContainer, settingsInfoPanel, "Settings", "cog.png");


		tabContainer.setSelectedIndex(0);


		myPanel.add(tabContainerFg, JLayeredPane.PALETTE_LAYER);
		myPanel.add(tabContainer, JLayeredPane.DEFAULT_LAYER);
		tabContainer.setBounds(0, -2, myPanel.getWidth() + 1, myPanel.getHeight() + 1);

		String MyIP = DudesFunctions.Network.getMyIPAddress();
		String ServerIP = DudesConfig.serverAddr;
		int ScenarioID = 0;
		String ScenarioName = "Sim1";

		// Declare label MyIP
		JLabel lblMyIP = new JLabel("My IP:", JLabel.LEFT);
		lblMyIP.setBounds(3, 1, DudesFunctions.GUI.stringWidth("My IP"), DudesFunctions.GUI.stringHeight("G"));

		// Declare label ShowMyIP
		JLabel lblShowMyIP = new JLabel(MyIP, JLabel.LEFT);
		lblShowMyIP.setBounds(103, 1, DudesFunctions.GUI.stringWidth(MyIP), DudesFunctions.GUI.stringHeight("G"));

		// Declare label ServerIP
		JLabel lblServerIP = new JLabel("Server IP:", JLabel.LEFT);
		lblServerIP.setBounds(3, 15, DudesFunctions.GUI.stringWidth("Server IP:"), DudesFunctions.GUI.stringHeight("G"));

		// Declare label ShowServerIP
		JLabel lblShowServerIP = new JLabel(ServerIP, JLabel.LEFT);
		lblShowServerIP.setBounds(103, 15, DudesFunctions.GUI.stringWidth(ServerIP), DudesFunctions.GUI.stringHeight("G"));

		// Declare label ScenarioID
		JLabel lblScenarioID = new JLabel("Scenario ID:", JLabel.LEFT);
		lblScenarioID.setBounds(3, 29, DudesFunctions.GUI.stringWidth("Scenario ID:") + 10, DudesFunctions.GUI.stringHeight("G"));

		// Declare label ShowScenarioID
		JLabel lblShowScenarioID = new JLabel(Integer.toString(ScenarioID), JLabel.LEFT);
		lblShowScenarioID.setBounds(103, 29, DudesFunctions.GUI.stringWidth(Integer.toString(ScenarioID)), DudesFunctions.GUI.stringHeight("G"));

		// Declare label ScenarioName
		JLabel lblScenarioName = new JLabel("Scenario Name:", JLabel.LEFT);
		lblScenarioName.setBounds(3, 43, DudesFunctions.GUI.stringWidth("Scenario Name:"), DudesFunctions.GUI.stringHeight("G"));

		// Declare label ShowScenarioName
		JLabel lblShowScenarioName = new JLabel(ScenarioName, JLabel.LEFT);
		lblShowScenarioName.setBounds(103, 43, DudesFunctions.GUI.stringWidth(ScenarioName) + 10, DudesFunctions.GUI.stringHeight("G"));

		// Declare label TimeRunning
		JLabel lblTimeRunning = new JLabel("Time running:", JLabel.LEFT);
		lblTimeRunning.setBounds(3, 57, DudesFunctions.GUI.stringWidth("Time running:"), DudesFunctions.GUI.stringHeight("G"));

		// Place labels in ServerInfoPanel
		settingsInfoPanel.add(lblMyIP);
		settingsInfoPanel.add(lblShowMyIP);
		settingsInfoPanel.add(lblServerIP);
		settingsInfoPanel.add(lblShowServerIP);
		settingsInfoPanel.add(lblScenarioID);
		settingsInfoPanel.add(lblShowScenarioID);
		settingsInfoPanel.add(lblScenarioName);
		settingsInfoPanel.add(lblShowScenarioName);
		settingsInfoPanel.add(lblTimeRunning);
		Painter.repaint(myPanel);

		/* ProjectileInt nuke = new ProjectileInt();
		 * nuke.setName("Little Boy");
		 * nuke.setAltitude(0);
		 * nuke.setType(PhysicalObjectType.PROJECTILE);
		 * nuke.setProjectileType(ProjectileType.MISSILE_GUIDED);
		 * nuke.setDirection(35.0);
		 * nuke.setPosition(new DoublePoint(74.0, 52.0));
		 * nuke.setTarget(ClientApp.SharedVars.getMyVehicle());
		 * nuke.setSource(new DoublePoint(54.0, 30.0));
		 * ClientApp.SharedVars.setSelectedItemOnMap(nuke); */
		// System.out.println(ClientApp.SharedVars.getSelectedItemOnMap().toString());
		int marge = 16;
		PhysicalObject selectedObject = null;
		PhysicalObjectInt selectedPhysicalObject = null;
		String projectileType = "No object selected";
		String vehicleType = "No object selected";
		String objectType = "No object selected";
		String objectName = "No object selected";
		DoublePoint objectPosition = new DoublePoint(0.0, 0.0);
		int objectAltitude = 0;
		Double selectedObjectDirection = 0.0;
		Double selectedObjectSpeed = 0.0;
		String projectileOrigin = "No origin available";
		VehicleInt projectileTargetObject = null;
		String projectileTarget = "";
		Object sysRemote;

		JLabel lblProjectileType = new JLabel();
		JLabel lblShowProjectileType = new JLabel();
		JLabel lblProjectileOrigin = new JLabel();
		JLabel lblShowProjectileOrigin = new JLabel();
		JLabel lblProjectileTarget = new JLabel();
		JLabel lblShowProjectileTarget = new JLabel();

		JLabel lblVehicleType = new JLabel();
		JLabel lblShowVehicleType = new JLabel();
		JLabel lblObjectType = new JLabel();
		JLabel lblShowObjectType = new JLabel();
		JLabel lblObjectName = new JLabel();
		JLabel lblShowObjectName = new JLabel();
		JLabel lblObjectPosition = new JLabel();
		JLabel lblShowObjectPosition = new JLabel();
		JLabel lblObjectAltitude = new JLabel();
		JLabel lblShowObjectAltitude = new JLabel();
		JLabel lblObjectDirection = new JLabel();
		JLabel lblShowObjectDirection = new JLabel();
		JLabel lblObjectSpeed = new JLabel();
		JLabel lblShowObjectSpeed = new JLabel();

		objectInfoPanel.add(lblObjectType);
		objectInfoPanel.add(lblShowObjectType);
		objectInfoPanel.add(lblObjectName);
		objectInfoPanel.add(lblShowObjectName);
		objectInfoPanel.add(lblObjectAltitude);
		objectInfoPanel.add(lblShowObjectAltitude);
		objectInfoPanel.add(lblObjectDirection);
		objectInfoPanel.add(lblShowObjectDirection);
		objectInfoPanel.add(lblObjectSpeed);
		objectInfoPanel.add(lblShowObjectSpeed);
		objectInfoPanel.add(lblObjectPosition);
		objectInfoPanel.add(lblShowObjectPosition);


		while (true) {

			if (ClientApp.SharedVars.getSelectedItemOnMap() != null) {
				selectedObject = (PhysicalObject) ClientApp.SharedVars.getSelectedItemOnMap();
				objectPosition = selectedObject.getPosition();
				objectAltitude = selectedObject.getAltitude();
				selectedObjectDirection = selectedObject.getDirection();
				selectedObjectSpeed = selectedObject.getSpeed();
				selectedPhysicalObject = selectedObject;

				if (selectedObject.getType() == PhysicalObjectType.PROJECTILE) {
					ProjectileInt selectedProjectile = (ProjectileInt) selectedPhysicalObject;
					ProjectileType selectedProjectileType = selectedProjectile.getProjectileType();
					objectName = selectedProjectile.getName();
					objectType = "Projectile";
					projectileOrigin = selectedProjectile.getSource().toString();
					// projectileTargetObject = (Vehicle) selectedProjectile.getTarget();

					sysRemote = selectedProjectile.getTarget();
					projectileTargetObject = (VehicleInt) TransparentItemProxy.getItem(sysRemote, new Class[] {
							VehicleInt.class
					}
							);
					// projectileTarget = projectileTargetObject.getName();
					// projectileOrigin = selectedProjectile.getSource().toString();

					switch (selectedProjectileType) {
						case UNKNOWN:
							projectileType = "Unknown";
						case MISSILE_GUIDED:
							projectileType = "Guided missile";
						case MISSILE_UNGUIDED:
							projectileType = "Unguided missile";
						case TORPEDO:
							projectileType = "Torpedo";
					}
					// Declare label ProjectileType
					lblProjectileType.setText("Projectiletype:");
					lblProjectileType.setBounds(2, marge, DudesFunctions.GUI.stringWidth("Projectiletype:"), DudesFunctions.GUI.stringHeight("G"));

					// Declare label showProjectileType
					lblShowProjectileType.setText(projectileType);
					lblShowProjectileType.setBounds(85, marge, DudesFunctions.GUI.stringWidth(projectileType), DudesFunctions.GUI.stringHeight("G"));

					// Declare label Origin
					lblProjectileOrigin.setText("Origin:");
					lblProjectileOrigin.setBounds(2, marge * 7, DudesFunctions.GUI.stringWidth("Origin:"), DudesFunctions.GUI.stringHeight("G"));

					// Declare label showOrigin
					lblShowProjectileOrigin.setText(projectileOrigin);
					lblShowProjectileOrigin.setBounds(85, marge * 7, DudesFunctions.GUI.stringWidth(projectileOrigin), DudesFunctions.GUI.stringHeight("G"));

					// Declare label Target
					lblProjectileTarget.setText("Target:");
					lblProjectileTarget.setBounds(2, marge * 8, DudesFunctions.GUI.stringWidth("Target:"), DudesFunctions.GUI.stringHeight("G"));

					// Declare label Target
					lblShowProjectileTarget.setText(projectileTarget);
					lblShowProjectileTarget.setBounds(85, marge * 8, DudesFunctions.GUI.stringWidth(projectileTarget), DudesFunctions.GUI.stringHeight("G"));

					objectInfoPanel.add(lblProjectileType);
					objectInfoPanel.add(lblShowProjectileType);
					objectInfoPanel.add(lblProjectileOrigin);
					objectInfoPanel.add(lblProjectileTarget);
					objectInfoPanel.add(lblShowProjectileOrigin);
					objectInfoPanel.add(lblShowProjectileTarget);

				} else if (selectedObject.getType() == PhysicalObjectType.VEHICLE) {
					VehicleInt selectedVehicle = (VehicleInt) selectedObject;
					VehicleType selectedVehicleType = selectedVehicle.getVehicleType();
					objectName = selectedVehicle.getName();
					objectType = "Vehicle";

					switch (selectedVehicleType) {
						case FRIGATE:
							vehicleType = "Frigate";
						case UNKNOWN:
							vehicleType = "Unknown";
						case SUBMARINE:
							vehicleType = "Submarine";
						case CONVOY:
							vehicleType = "Convoy";

					}

					// Declare label VehicleType
					lblVehicleType.setText("Vehicletype:");
					lblVehicleType.setBounds(2, marge, DudesFunctions.GUI.stringWidth("Vehicletype:"), DudesFunctions.GUI.stringHeight("G"));

					// Declare label showVehicleType
					lblShowVehicleType.setText(vehicleType);
					lblShowVehicleType.setBounds(85, marge, DudesFunctions.GUI.stringWidth(vehicleType), DudesFunctions.GUI.stringHeight("G"));

					objectInfoPanel.add(lblVehicleType);
					objectInfoPanel.add(lblShowVehicleType);

				}

				// Declare label ObjectType
				lblObjectType.setText("Objecttype:");
				lblObjectType.setBounds(2, 0, DudesFunctions.GUI.stringWidth("Objecttype:"), DudesFunctions.GUI.stringHeight("G"));

				// Declare label showObjectType
				lblShowObjectType.setText(objectType);
				lblShowObjectType.setBounds(85, 0, DudesFunctions.GUI.stringWidth(objectType), DudesFunctions.GUI.stringHeight("G"));

				// Declare label ObjectName
				lblObjectName.setText("Name:");
				lblObjectName.setBounds(2, marge * 2, DudesFunctions.GUI.stringWidth("Name:"), DudesFunctions.GUI.stringHeight("G"));

				// Declare label showObjectName
				lblShowObjectName.setText(objectName);
				lblShowObjectName.setBounds(85, marge * 2, DudesFunctions.GUI.stringWidth(objectName), DudesFunctions.GUI.stringHeight("G"));

				// Declare label ObjectPosition
				lblObjectPosition.setText("Position:");
				lblObjectPosition.setBounds(2, marge * 3, DudesFunctions.GUI.stringWidth("Position:"), DudesFunctions.GUI.stringHeight("G"));

				// Declare label showObjectPosition
				lblShowObjectPosition.setText(objectPosition.toString());
				lblShowObjectPosition.setBounds(85, marge * 3, DudesFunctions.GUI.stringWidth(objectPosition.toString()), DudesFunctions.GUI.stringHeight("G"));

				// Declare label ObjectAltitude
				lblObjectAltitude.setText("Altitude:");
				lblObjectAltitude.setBounds(2, marge * 4, DudesFunctions.GUI.stringWidth("Altitude:"), DudesFunctions.GUI.stringHeight("G"));

				// Declare label showObjectAltitude
				lblShowObjectAltitude.setText(Integer.toString(objectAltitude));
				lblShowObjectAltitude.setBounds(85, marge * 4, DudesFunctions.GUI.stringWidth(Integer.toString(objectAltitude)), DudesFunctions.GUI.stringHeight("G"));

				// Declare label ObjectDirection
				lblObjectDirection.setText("Direction:");
				lblObjectDirection.setBounds(2, marge * 5, DudesFunctions.GUI.stringWidth("Direction:"), DudesFunctions.GUI.stringHeight("G"));

				// Declare label showObjectDirection
				lblShowObjectDirection.setText(selectedObjectDirection.toString());
				lblShowObjectDirection.setBounds(85, marge * 5, DudesFunctions.GUI.stringWidth(selectedObjectDirection.toString()), DudesFunctions.GUI.stringHeight("G"));

				// Declare label ObjectSpeed
				lblObjectSpeed.setText("Speed:");
				lblObjectSpeed.setBounds(2, marge * 6, DudesFunctions.GUI.stringWidth("Speed:"), DudesFunctions.GUI.stringHeight("G"));

				// Declare label showObjectSpeed
				lblShowObjectSpeed.setText(selectedObjectSpeed.toString());
				lblShowObjectSpeed.setBounds(85, marge * 6, DudesFunctions.GUI.stringWidth(selectedObjectSpeed.toString()), DudesFunctions.GUI.stringHeight("G"));

			}
			Painter.repaint(myPanel);
		}
	}
}
